//
//  CS_Page_BulePintDeviceView.m
//  Pods
//
//  Created by 网新中研 on 2017/8/2.
//
//

#import "CS_Page_BulePintDeviceView.h"

@interface CS_Page_BulePintDeviceView()

//打印按钮
@property (nonatomic, strong) UIButton *printButton;

//连接图片
@property (nonatomic, strong) UIImageView *connectImageView;

//设备名称
@property (nonatomic, strong) UILabel *deviceLabel;

//选择设备
@property (nonatomic, strong) UIButton *setDeviceButton;

//是否可打印
@property (nonatomic, assign) BOOL isPrintAvailable;

@property (nonatomic, strong) NSString *deviceName;

//点击打印
@property (nonatomic, copy) PrintClickBlock printClickHander;

//点击查找设备处理
@property (nonatomic, copy) FindDeviceClickBlock findDeviceClickHandler;

@property (nonatomic, strong) CS_Pop_Progress *pop_Progress;


@property (nonatomic, assign) CGFloat resetWidth;
@property (nonatomic, assign) CGFloat leftMargin;

@property (nonatomic, strong) UIView *seperatorLine;

@end

@implementation CS_Page_BulePintDeviceView

+(id)page_BluePrintDeviceView:(FindDeviceClickBlock)findDeviceClickHandler withPrintClickHandler:(FindDeviceClickBlock)printClickHandler{
    
    CS_Page_BulePintDeviceView *page_BluePrintDeviceView = [CS_Page_BulePintDeviceView new];
    page_BluePrintDeviceView.findDeviceClickHandler = findDeviceClickHandler;
    page_BluePrintDeviceView.printClickHander = printClickHandler;
    return page_BluePrintDeviceView;
    
}

#define Print_MARGIN_LEFT 10.0

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    CGFloat marginLeft = Print_MARGIN_LEFT;
    
    CGFloat primaryX = marginLeft;
    
    //打印按钮
    self.printButton.frame = CGRectMake(primaryX, 10, 60, 30);
    primaryX += 60+20;
    [self addSubview:self.printButton];
    
    self.leftMargin = primaryX;
    self.resetWidth = kWindowW - 70 - 10 - primaryX;
    
    CGFloat needWidth = [self estimateWith:self.deviceLabel.text font:self.deviceLabel.font height:30];
    if (needWidth < self.resetWidth - 30 - 20) {
        primaryX = self.leftMargin - 20 + (self.resetWidth + 20.0 - 30 - needWidth) / 2.0;
    }
    
    //设备图片
    self.connectImageView.frame = CGRectMake(primaryX, 12, 25, 25);
    primaryX += 25 + 5;
    [self addSubview:self.connectImageView];
    
    //设备名称
    self.deviceLabel.frame = CGRectMake(primaryX, 10, kWindowW - primaryX - 70 - 10 - marginLeft, 30);
    [self addSubview:self.deviceLabel];
    
    //查找设备
    self.setDeviceButton.frame = CGRectMake(kWindowW - marginLeft - 70, 10, 70, 30);
    [self addSubview:self.setDeviceButton];
    
    self.frame = CGRectMake(0, primaryY, kWindowW, 50);
    
    self.seperatorLine.frame = CGRectMake(0, 0, kWindowW, 1);
    
    [self addSubview:self.seperatorLine];
    
    self.backgroundColor = [UIColor whiteColor];
    
    [superView addSubview:self];
    
    return primaryY + 60;
}

/**
 * 设置所找到的设备
 */
-(void)setDevice:(NSString *)deviceName{
    
    _deviceName = deviceName;
    
    if ([self isNullOrEmpty:deviceName]) {
        //设备未连接
        [self styleUnconnected];
    }else{
        self.deviceLabel.text = deviceName;
        [self styleConnected];
        
    }
    
}

/**
 * 设置打印结束
 */
-(void)setPrintFinished{
    
    [self.pop_Progress.superKLCPopup dismiss:YES];
    
    [self styleConnected];
    
}

-(void)printClick{
    
    if (self.isPrintAvailable) {
        if (self.printClickHander) {
            self.printClickHander();
        }
    }
}

-(void)findDeviceClick{
    if (self.findDeviceClickHandler) {
        self.findDeviceClickHandler();
    }
}

//返回设备名称
-(NSString *)value{
    return _deviceName;
}

-(void)styleUnconnected{
    
    self.isPrintAvailable = NO;
    
    _printButton.backgroundColor = COLOR_TEXT_3;
    [_printButton setTitle:@"打印" forState:UIControlStateNormal];
    
    _deviceLabel.textColor = COLOR_TEXT_3;
    _deviceLabel.text = @"目标设备(未连接)";
    
    CGFloat primaryX = self.leftMargin;
    CGFloat needWidth = [self estimateWith:self.deviceLabel.text font:self.deviceLabel.font height:30];
    if (needWidth < self.resetWidth - 30 - 20) {
        primaryX = self.leftMargin - 20 + (self.resetWidth + 20.0 - 30 - needWidth) / 2.0;
    }
    
    //设备图片
    self.connectImageView.frame = CGRectMake(primaryX, 12, 25, 25);
    primaryX += 25 + 5;
    [self addSubview:self.connectImageView];
    
    //设备名称
    self.deviceLabel.frame = CGRectMake(primaryX, 10, kWindowW - primaryX - 70 - 10 - Print_MARGIN_LEFT, 30);
    [self addSubview:self.deviceLabel];
    
    _connectImageView.image = [UIImage imageNamed:@"print_wlj"];
    
    //设置查找设备按钮
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"查找设备"];
    [attributeString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:(NSRange){0, [attributeString length]}];
    [attributeString addAttribute:NSForegroundColorAttributeName value:COLOR_THEME range:NSMakeRange(0, [attributeString length])];
    [attributeString addAttribute:NSUnderlineColorAttributeName value:COLOR_THEME range:NSMakeRange(0, [attributeString length])];
    [_setDeviceButton setAttributedTitle:attributeString forState:UIControlStateNormal];
}

-(void)styleConnected{
    
    self.isPrintAvailable = YES;
    
    _printButton.backgroundColor = COLOR_THEME;
    CGFloat primaryX = self.leftMargin;
    
    CGFloat needWidth = [self estimateWith:self.deviceLabel.text font:self.deviceLabel.font height:30];
    if (needWidth < self.resetWidth - 30 - 20) {
        primaryX = self.leftMargin - 20 + (self.resetWidth + 20.0 - 30 - needWidth) / 2.0;
    }
    
    //设备图片
    self.connectImageView.frame = CGRectMake(primaryX, 12, 25, 25);
    primaryX += 25 + 5;
    [self addSubview:self.connectImageView];
    
    //设备名称
    self.deviceLabel.frame = CGRectMake(primaryX, 10, kWindowW - primaryX - 70 - 10 - Print_MARGIN_LEFT, 30);
    [self addSubview:self.deviceLabel];
    
    [_printButton setTitle:@"打印" forState:UIControlStateNormal];
    
    _deviceLabel.textColor = COLOR_THEME;
    
    _connectImageView.image = [UIImage imageNamed:@"print_ylj"];
    
    //设置查找设备按钮
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"更改设备"];
    [attributeString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:(NSRange){0, [attributeString length]}];
    [attributeString addAttribute:NSForegroundColorAttributeName value:COLOR_THEME range:NSMakeRange(0, [attributeString length])];
    [attributeString addAttribute:NSUnderlineColorAttributeName value:COLOR_THEME range:NSMakeRange(0, [attributeString length])];
    [_setDeviceButton setAttributedTitle:attributeString forState:UIControlStateNormal];
    
}

-(UIButton *)printButton{
    if (_printButton == nil) {
        _printButton = [UIButton new];
        [_printButton addTarget:self action:@selector(printClick) forControlEvents:UIControlEventTouchUpInside];
        [_printButton setTitle:@"打印" forState:UIControlStateNormal];
        _printButton.titleLabel.font = [UIFont boldSystemFontOfSize:15.0];
        STYLE_SAVE_BUTTON(_printButton);
        _printButton.backgroundColor = COLOR_TEXT_3;
    }
    return _printButton;
}

-(UIImageView *)connectImageView{
    if (_connectImageView == nil) {
        _connectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"print_wlj"]];
    }
    return _connectImageView;
}

-(UILabel *)deviceLabel{
    if (_deviceLabel == nil) {
        _deviceLabel = [[UILabel alloc] init];
        _deviceLabel.textColor = COLOR_TEXT_3;
        _deviceLabel.font = [UIFont boldSystemFontOfSize:14.0];
        _deviceLabel.textAlignment = NSTextAlignmentLeft;
        _deviceLabel.text = @"目标设备(未连接)";
    }
    return _deviceLabel;
}

-(UIButton *)setDeviceButton{
    if (_setDeviceButton == nil) {
        _setDeviceButton = [[UIButton alloc] init];
        [_setDeviceButton setTitleColor:COLOR_THEME forState:UIControlStateNormal];
        _setDeviceButton.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
        
        [_setDeviceButton addTarget:self action:@selector(findDeviceClick) forControlEvents:UIControlEventTouchUpInside];
        
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"查找设备"];
        [attributeString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:(NSRange){0, [attributeString length]}];
        [attributeString addAttribute:NSForegroundColorAttributeName value:COLOR_THEME range:NSMakeRange(0, [attributeString length])];
        [attributeString addAttribute:NSUnderlineColorAttributeName value:COLOR_THEME range:NSMakeRange(0, [attributeString length])];
        [_setDeviceButton setAttributedTitle:attributeString forState:UIControlStateNormal];
    }
    return _setDeviceButton;
}

-(CS_Pop_Progress *)pop_Progress{
    if (_pop_Progress == nil) {
        _pop_Progress = [CS_Pop_Progress new];
    }
    return _pop_Progress;
}

-(UIView *)seperatorLine{
    if (_seperatorLine == nil) {
        _seperatorLine = [UIView new];
        STYLE_SEPERATOR_LINE(_seperatorLine);
    }
    return _seperatorLine;
}

@end
